<template>
  <div class="InvitationCode" :style="'min-height: ' + docheight + 'px'">
    <div v-if="!showTimeOut&&showCont">
      <div class="codeUserCont">
        <div class="codeUserTitle">
          <div>接单记录</div>
          <div class="timeBtn">
            <span v-if="startDate && endDate">
              <span @click="showDate = true"
                >{{ startDate }}～{{ endDate }}</span
              >
              <span style="color:#3273f6" @click="czTime">重置日期</span>
            </span>
            <span v-else
              >订单日期:<span @click="showDate = true" style="color:#3273f6"
                >未选择</span
              ></span
            >
          </div>
        </div>
        <div class="codeUserList">
          <div
            class="codeUserItem"
            :key="index"
            v-for="(item, index) in userArr"
          >
            <div class="topCont">
              <img :src="item.package_cover" />
              <div class="userInfo">
                <div class="userName">
                  <span class="desTag pinkTag" v-if="item.order_status==3">待服务</span>
                  <span class="desTag greenTag" v-if="item.order_status==5">已完成</span>
                  <span class="desTag blacktag" v-if="item.order_status==7">已取消</span>
                  <span>{{ item.package_name }}</span>
                </div>
                <div class="priceCont">
                  <div class="priceAll">最低到手：¥{{ item.cleaning_commission }}</div>
                </div>
                <div class="goodsType">
                  <div class="goodsTypeItem">{{ item.package_rule_name }}</div>
                </div>
              </div>
            </div>
            <div class="remarkVal">核销时间:{{ item.check_time }}</div>
            <div class="remarkVal">预约时间:{{ item.service_time }}</div>
            <div class="remarkVal">服务地址:{{ item.service_address }} {{ item.house_number }}</div>
            <div class="remarkVal flexText">
              <div>联系电话:{{ item.user_phone }}({{ item.user_name }})</div>
              <div class="telText" @click="telFn(item.user_phone)" v-if="item.showPhone">拨号</div>
            </div>
            <div class="remarkVal">订单备注:{{ item.service_remark?item.service_remark:'无备注' }}</div>
          </div>
          <div class="nodataTip" v-if="userArr.length == 0">
            你还没有订单记录哦～
          </div>
          <div class="nodataTip" v-if="userArr.length>0">{{ loadMoreText }}</div>
        </div>
      </div>
      <sub-load v-if="showLoad"></sub-load>
    </div>
    <van-calendar
      ref="calend"
      :min-date="minDate"
      :max-date="maxDate"
      color="#3273f6"
      v-model:show="showDate"
      type="range"
      @confirm="onConfirm"
    />
    <toast
      v-model="showToast"
      position="middle"
      width="50%"
      type="text"
      :time="1000"
      >{{ showText }}</toast
    >
    <footcpm></footcpm>
    <getLoad v-if="!showCont"></getLoad>
    <getTimeOut v-if="showTimeOut"></getTimeOut>
  </div>
</template>

<script>
import { Toast } from "vux";
import SaveBtn from "@/components/public/saveBtn.vue";
import subLoad from "@/components/public/subLoad.vue";
import getLoad from "@/components/public/getLoad";
import getTimeOut from "@/components/public/getTimeOut";
import footcpm from "@/components/footcpm";
import { Receiving } from "@/http/api";
import bus from '@/assets/evnetBus.js'

export default {
  components: { getTimeOut, getLoad, subLoad, Toast, SaveBtn, footcpm },
  data() {
    return {
      showTimeOut: false,
      showCont: false,
      showLoad: false,
      showText: "",
      loadMoreText:'数据加载中～',
      showToast: false,
      showDate: false,
      userArr: [],
      minDate: new Date(2022, 0, 1),
      maxDate: new Date(),
      startDate: "",
      endDate: "",
      page: 1,
      per_page: 4,
      last_page: 0,
      getting: false,
    };
  },
  props: {},
  methods: {
    getTimeStr(time) {
      let n = time.getFullYear();
      let y = time.getMonth() + 1;
      let d = time.getDate();
      let timeStr = n + "-" + y + "-" + d;
      return timeStr;
    },
    czTime() {
      this.endDate = "";
      this.startDate = "";
      this.page = 1;
      this.last_page = 0;
      this.userArr = [];
      this.$refs.calend.reset();
      this.getData();
    },
    telFn(num){
      window.location.href = "tel:"+num
    },
    onConfirm(values) {
      const [start, end] = values;
      this.startDate = this.getTimeStr(values[0]);
      this.endDate = this.getTimeStr(values[1]);
      this.showDate = false;
      this.userArr = [];
      this.page = 1;
      this.last_page = 0;
      this.getData();
    },
    getData() {
      let time = "";
      if (this.startDate != "" && this.endDate != "") {
        time = this.startDate + "," + this.endDate;
      }
      let data = {
        page: this.page,
        per_page: this.per_page,
        time: time
      };
      Receiving(data)
        .then(res => {
          this.showCont = true;
          if (res.data.code == 1) {
            let arr = res.data.data.data
            for(let i=0;i<arr.length;i++){
              arr[i]['showPhone'] = true
              if(arr[i].user_phone.indexOf('*')>-1){
                arr[i]['showPhone'] = false
              }
              this.userArr.push(arr[i])
            }
            setTimeout(() => {
              this.getting = false
            }, 100);
            this.last_page = res.data.data.last_page
            if(this.page==this.last_page){
              this.loadMoreText = '到底啦～'
            }else{
              this.loadMoreText = '数据加载中~'
            }
          } else {
            this.showToast = true;
            this.showText = res.data.msg
          }
        })
        .catch(() => {
          this.showTimeOut = true;
          this.showCont = true;
        });
    },
    nextfn() {
      if (this.$route.path == "/history") {
        let h =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        let wh =
          document.documentElement.clientHeight || document.body.clientHeight;
        let top = document.documentElement.scrollTop || document.body.scrollTop;
        let bottom = h - wh - top;
        if (bottom <= 50) {
          if (this.getting == false) {
            if (this.last_page > this.page) {
              this.getting = true;
              this.page++;
              this.getData();
            }
          }
        }
      }
    }
  },
  mounted() {
    window.addEventListener("scroll", this.nextfn);
  },
  created() {
    this.getData();
  }
};
</script>

<style scoped lang="less">
.InvitationCode {
  width: 100%;
  background-color: #f7f8fa;
  box-sizing: border-box;
  padding-bottom: 3.5rem;
  padding-top: 1rem;
  .codeUserCont {
    width: 92%;
    margin: 0 auto;
    background-color: #fff;
    padding: 0.75rem;
    box-sizing: border-box;
    border-radius: 0.25rem;
    .codeUserTitle {
      font-size: 0.8rem;
      font-weight: bold;
      padding: 0.75rem 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .timeBtn {
        font-weight: normal;
        font-size: 0.65rem;
      }
    }
    .codeUserList {
      padding: 0.75rem 0;
      .codeUserItem {
        width: 100%;
        padding: 0.5rem 0 0.75rem;
        border-bottom: 1px solid #ebebeb;
        .topCont{
          display: flex;
          padding: 0.5rem 0;
          align-items: center;
          img {
            margin-right: 0.75rem;
            width: 4.5rem;
            border-radius: 5px;
        }
        .userInfo {
          width: 2.5rem;
          flex: 1;
          .userName {
            font-size: 0.7rem;
            font-weight: bold;
            color: #333;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 100%;
            .desTag {
              padding: 0.1rem 0.5rem;
              background-color: #3273f6;
              font-size: 0.6rem;
              color: #fff;
              border-radius: 0.1rem;
              font-weight: normal;
            }
            .pinkTag{
              color: #fff;
              background-color: #F56C6C;
            }
            .greenTag{
              background-color: #85CE61;
            }
            .blacktag{
              color: #666;
              background-color: #ebebeb;
            }
          }
          .priceCont{
            align-items: center;
            display: flex;
            font-size: 0.6rem;
            color: #666;
            margin-top: 0.5rem;
            .priceSub{
              margin-left: 0.75rem;
            }
          }
          .goodsType{
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 0.35rem;
            .goodsNumCont{
              display: flex;
              height: 1rem;
              line-height: 1rem;
              color: #999;
              font-size: 0.6rem;
              align-items: center;
            }
            .goodsTypeItem{
              padding:0.1rem 0.5rem;
              background-color: #3273f6;
              font-size: 0.6rem;
              height: 1rem;
              line-height: 1rem;
              border-radius: 0.75rem;
              color: #fff;
            }
          }
          .userTime {
            font-size: 0.6rem;
            margin-top: 0.25rem;
            color: #666;
          }
        }
        }
        .underCont{
          display: flex;
          align-items: center;
          justify-content: flex-end;
          margin-top: 0.5rem;
          .orderBtn{
            padding: 0.35rem 1.8rem;
            border: 1px solid #3273f6;
            color: #3273f6;
            font-size: 0.6rem;
            margin-left: 0.5rem;
            border-radius: 0.25rem;
          }
          .sureBtn{
            background-color: #3273f6;
            color: white;
          }
        }
        .remarkVal{
          color: #666;
          font-size: 0.6rem;
          margin-top: 0.5rem;
        }
        .flexText{
          display: flex;
          justify-content: space-between;
          align-items: center;
          .telText{
            color: #3273f6;
            text-decoration: underline;
          }
        }
      }
      .nodataTip {
        color: #666;
        font-size: 0.6rem;
        text-align: center;
        margin-top: 20px;
      }
    }
  }
  .orderMask{
    padding: 0.75rem;
    box-sizing: border-box;
    .maskTitle{
      font-size: 0.8rem;
      font-weight: bold;
      text-align: center;
    }
    .maskTip{
      margin-top: 0.5rem;
      line-height: 1.1rem;
    }
  }
}
</style>
<style>
.InvitationCode .van-cell::after {
  border-bottom: none;
}
</style>
